package yancy.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import yancy.common.annotation.SysLog;
import yancy.common.utils.Constant;
import yancy.common.utils.PageUtils;
import yancy.common.utils.R;
import yancy.common.validator.ValidatorUtils;
import yancy.common.validator.group.AddGroup;
import yancy.modules.sys.entity.SysBook;
import yancy.modules.sys.entity.SysBorrowBook;
import yancy.modules.sys.entity.SysBorrowLog;
import yancy.modules.sys.service.SysBookAuthorService;
import yancy.modules.sys.service.SysBookService;
import yancy.modules.sys.service.SysBorrowBookService;
import yancy.modules.sys.service.SysBorrowLogService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 还书管理
 */
@RestController
@RequestMapping("/sys/return")
@AllArgsConstructor
public class ReturnController extends AbstractController {

    private final SysBookService SysBookService;
    private final SysBookService bookService;
    private final SysBorrowBookService borrowBookService;
    private final SysBookAuthorService bookAuthorService;
    private final SysBorrowLogService borrowLogService;


    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
        }
        Page page = borrowBookService.query(getPage(params), params);
        PageUtils pageUtils = new PageUtils(page);


        return R.ok().put("page", pageUtils);
    }


    /**
     * 用户信息
     */
    @GetMapping("/info/{bookId}")
    public R info(@PathVariable("bookId") Long bookId) {
        SysBook book = bookService.getById(bookId);

        //获取用户所属的角色列表
        List<Long> bookIdList = bookAuthorService.queryAuthorIdList(bookId);
        book.setAuthorIdList(bookIdList);

        return R.ok().put("book", book);
    }


    /**
     * 保存用户
     */
    @SysLog("保存图书")
    @PostMapping("/save")
    public R save(@RequestBody SysBook book) {
        ValidatorUtils.validateEntity(book, AddGroup.class);

        book.setCreateUserId(getUserId());
        book.setCreateTime(new Date());
        bookService.saveBook(book);

        return R.ok();
    }

    /**
     * return book
     *
     * @param borrowId    borrowId
     * @param returnCount the sysBorrowBook object containing the count to be returned
     * @return 0 otherwise 500
     */
    @PostMapping("/update/{borrowId}")
    public R returnBook(@PathVariable("borrowId") Long borrowId, @RequestBody SysBorrowBook returnCount) {
        SysBorrowBook borrow = borrowBookService.getById(borrowId);
        if (returnCount.getTotal() > borrow.getNeedBack()) {
            return R.error("请输入正确的书籍数量");
        }
        borrow.returnBook(returnCount.getTotal());
        borrowBookService.updateById(borrow);

        // save relevant operational into log.
        borrowLogService.save(SysBorrowLog.OfBack(getUserId(), borrow.getBookId(), returnCount.getTotal().intValue(), getUserId()));
        return R.ok();
    }


    /**
     * 删除用户
     */
    @SysLog("删除用户")
    public R delete(@RequestBody Long[] bookIds) {

        return R.ok();
    }

}
